/* @Description:midway-back/src/model/openMenuindex.ts @author:378237242@qq.com @date:2023/12/19 */
import { zip } from "lodash";
import { findCommonElement } from "../../utils/array.utils";

const openMenu: Model = {
  name: "openMenu",
  cname: "打开菜单",
  // 关键字数组，主要是动作
  _actionKeywords: ["打开"],
  _actionKeyword: {},
  _actionKeywordsRule: function (analysisResult: AnalysisResult) {
    // 1.判断分词结果必须存在关键词
    // 2.关键词必须是动词v,必须是核心或直接宾语
    const keyword = findCommonElement(
      this._actionKeywords,
      analysisResult.word,
    );
    if (keyword) {
      const { indexInB } = keyword;
      if (
        analysisResult.pos[indexInB] === "v" &&
        (analysisResult.dp[indexInB].relate === "HED" ||
          analysisResult.dp[indexInB].relate === "VOB")
      ) {
        this._actionKeyword = keyword;
        return true;
      } else {
        return false;
      }
    } else {
      return false;
    }
  },
  // 简单动宾结构宾语
  _targetKeywords: ["菜单"],
  _targetKeyword: {},
  _targetKeywordsRule: function (analysisResult: AnalysisResult) {
    const keyword = findCommonElement(
      this._targetKeywords,
      analysisResult.word,
    );
    if (keyword) {
      const { indexInB } = keyword;
      if (
        analysisResult.pos[indexInB] === "n" &&
        (analysisResult.dp[indexInB].relate === "ATT" ||
          analysisResult.dp[indexInB].relate === "VOB")
      ) {
        this._targetKeyword = keyword;
        return true;
      } else {
        return false;
      }
    } else {
      return false;
    }
  },
  // 参数构造
  paramsStructure: [{ name: "menu", cname: "菜单", value: "" }],
  _paramsStructure: function (analysisResult: AnalysisResult) {
    const allATT = zip(
      analysisResult.word,
      analysisResult.pos,
      analysisResult.dp,
    ).slice(this._actionKeyword.indexInB + 1, this._targetKeyword.indexInB);
    this.paramsStructure[0].value = allATT.map((m) => m[0]).join("");
  },
};
export default openMenu;
